home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 3 / Cream of the Crop 3.iso / utility / secdrv.zip / CRYPTDSK.C < prev    next >
C/C++ Source or Header  |  1993-11-19  |  6KB  |  235 lines

  1. /* Secure Drive CRYPTDSK V1.0 */
  2. /* Encrypts/decrypts disks */
  3.  
  4. #include "secdrv.h"
  5.  
  6. void readseca(unsigned drive,unsigned head,unsigned track,unsigned sector,
  7.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  8. void writeseca(unsigned drive,unsigned head,unsigned track,unsigned sector,
  9.      unsigned nsects,unsigned secsize,unsigned char *buffer);
  10. void rstfsetup(void);
  11. void freebuf(void);
  12.  
  13. unsigned char *buf;
  14.  
  15. main()
  16. {
  17. unsigned drive,firstcyl,firsthead,encrypt;
  18. unsigned track,head,sector,maxtrack,maxhead,maxsector,secsize,t;
  19. unsigned serial[2];
  20. unsigned char key[16],check[4],iv[8];
  21. unsigned char *bufptr,*dummy;
  22. unsigned long i;
  23. char drvltr;
  24. word16 expkey[52];
  25.                    
  26. clrscr();
  27.  
  28. cryptdata=gettsradr();
  29.  
  30. if(cryptdata) {
  31.     cryptdata->fda.firstcyl=0x0ffff;
  32.     cryptdata->fdb.firstcyl=0x0ffff;
  33.     atexit(rstfsetup); }
  34.  
  35. if(!(buf=malloc(512))) {
  36.     printf("Error: unable to allocate memory for track buffer.\n");
  37.     exit(1); }
  38.  
  39. atexit(freebuf);
  40.  
  41. printf("\
  42. Secure Drive CryptDisk Version 1.0\n\
  43. \n\
  44. This program will encrypt or decrypt a floppy disk or hard drive\
  45.  partition.\n\
  46. \n");
  47.  
  48. askdrive:
  49. printf("Enter the letter of the drive to process, or X to enter the\n\
  50. drive, cylinder, and head manually, or Z to cancel: ");
  51. while(!isalpha(drvltr=toupper(getch())));
  52. printf("%c\n",drvltr);
  53.  
  54. if((drvltr>'B')&&cryptdata) {
  55.     printf("\nYou cannot encrypt or decrypt a hard drive partition \
  56. while\nthe TSR is resident in memory.\n");
  57.     exit(1); }
  58.  
  59. if(drvltr=='A') { firstcyl=0; firsthead=0; drive=0; }
  60. else if(drvltr=='B') { firstcyl=0; firsthead=0; drive=1; }
  61. else if(drvltr=='X') {
  62.     printf("\nEnter physical drive (0-1), cylinder, and head for the\
  63.  beginning\n\(boot sector) of this partition: ");
  64.     scanf("%u,%u,%u",&drive,&firstcyl,&firsthead);
  65.     drive+=0x80;
  66.     }
  67. else if(drvltr=='Z') { printf("\n"); exit(0); }
  68. else {
  69.     drive=255;
  70.     readptbl(0,0,0,drvltr,&drive,&firsthead,&firstcyl);
  71.     if(drive==255) {
  72.     printf("\nDrive not found.\n\n");
  73.     goto askdrive; }
  74.     printf("\nDrive %c is physical hard drive %u, head %u,\
  75.  cylinder %u\n\n",drvltr,drive,firsthead,firstcyl);
  76.     drive+=0x80;
  77.     }
  78.  
  79. if(drive<0x80) {
  80.    printf("\nInsert disk in drive %c and press any key to\
  81.  continue ",drvltr);
  82.    getch();
  83.    printf("\n\n"); }
  84.  
  85. readsec(drive,firsthead,firstcyl,1,1,buf);
  86. if((buf[510]!=0x55)||(buf[511]!=0xaa)) {
  87.     printf("This is not a boot sector.\n\n");
  88.     exit(1); }
  89.  
  90. encrypt=memcmp(buf+3,"CRYP",4);
  91.  
  92. calcdiskparams(buf,&maxtrack,&maxhead,&maxsector,
  93.            &secsize,serial);
  94.  
  95. printf("This disk has \
  96. %u tracks, %u sectors, %u heads, sector size %u bytes\n\n",
  97.        maxtrack+1,maxsector,maxhead,secsize);
  98.  
  99. if(buf=realloc(buf,maxsector*secsize))
  100.     printf("Allocated %u bytes for track buffer\n\n",
  101.         maxsector*secsize);
  102. else {
  103.     printf("Error: unable to allocate %u bytes for track buffer\n",
  104.         maxsector*secsize);
  105.     exit(1); }
  106.  
  107. if(encrypt)
  108.     {
  109.     printf("This disk is not encrypted. Do you want to encrypt it? ");
  110.     if(!getyn())
  111.     {
  112.     printf("\n");
  113.     exit(0);
  114.     }
  115.     getkey(key,check,TRUE);
  116.     }
  117. else
  118.     {
  119.     printf("This disk is encrypted. Do you want to decrypt it? ");
  120.     if(!getyn())
  121.     {
  122.     printf("\n");
  123.     exit(0);
  124.     }
  125.     for(t=0;t<3;t++) {
  126.     printf("\nEnter passphrase: "); 
  127.     getkey(key,check,FALSE);
  128.     if(!memcmp(check,buf+7,4)) break;
  129.     printf("Wrong passphrase.\n");
  130.     if(t==2) exit(0);
  131.     }
  132.     }
  133.  
  134. clrscr();
  135. printf("Last chance to abort. Continue? ");
  136. if(!getyn()) exit(1);
  137. en_key_idea((word16 *)key,expkey);
  138.  
  139. printf("\n");
  140. for(track=0;track<=maxtrack;track++)
  141.     for(head=0;head<maxhead;head++) {
  142.     if(track==0&&head<firsthead) head=firsthead;
  143.     gotoxy(1,4);
  144.     printf("Track %u, Head %u ",track,head);
  145.     readseca(drive,head,track+firstcyl,1,maxsector,secsize,buf);
  146.     bufptr=buf;
  147.     for(sector=1;sector<=maxsector;sector++) {
  148.         if(track==0&&head==firsthead&§or==1)
  149.         if(encrypt) {
  150.             memcpy(&buf[0x03],"CRYP",4);
  151.             memcpy(&buf[0x07],check,4); }
  152.         else
  153.             memcpy(&buf[0x03],"MSDOS   ",8);
  154.         else {
  155.         t=track+firstcyl;
  156.         iv[0]=t%256;
  157.         iv[1]=t/256;
  158.         iv[2]=head;
  159.         iv[3]=sector;
  160.         iv[4]=serial[0]%256;
  161.         iv[5]=serial[0]/256;
  162.         iv[6]=serial[1]%256;
  163.         iv[7]=serial[1]/256;
  164.         IDEACFB(iv,expkey,dummy,dummy,1);
  165.         if(encrypt)
  166.             IDEACFB(iv,expkey,bufptr,bufptr,secsize/8+1);
  167.         else    
  168.             IDEACFBX(iv,expkey,bufptr,bufptr,secsize/8+1);
  169.         }
  170.         bufptr+=secsize;
  171.         }
  172.     writeseca(drive,head,track+firstcyl,1,maxsector,secsize,buf);
  173.     }
  174.  
  175.  
  176. for(t=0;t<16;t++) key[t]='\0';
  177. for(t=0;t<52;t++) expkey[t]=0;
  178.  
  179. gotoxy(1,22);
  180. printf("\n\nDone.\n");
  181. return(0);
  182. }
  183.  
  184. void rstfsetup(void)
  185. {
  186. if(cryptdata) {
  187.     cryptdata->fda.firstcyl=0;
  188.     cryptdata->fdb.firstcyl=0; }
  189. }
  190.  
  191. void freebuf(void)
  192. {
  193. free(buf);
  194. }
  195.  
  196. void readseca(unsigned drive,unsigned head,unsigned track,unsigned sector,
  197.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  198. {
  199. unsigned i,j;
  200. char c;
  201. for(i=0;i<3;i++) 
  202.   if(!biosdisk(2,drive,head,track,sector,nsects,buffer)) return;
  203. printf("\nRead error: drive %02x, head %u, track %u\n",
  204.        drive,head,track);
  205. printf("Reading one sector at a time.\n");
  206. for(j=0;j<nsects;j++) {
  207.   for(i=0;i<3;i++) 
  208.     if(!biosdisk(2,drive,head,track,sector+j,1,buffer)) goto goodsec;
  209.   printf("Bad sector: drive %02x, head %u, track %u, sector %u\n",
  210.      drive,head,track,sector+j);
  211.   goodsec:
  212.   buffer+=secsize;
  213.   }    
  214. }
  215.  
  216. void writeseca(unsigned drive,unsigned head,unsigned track,unsigned sector,
  217.      unsigned nsects,unsigned secsize,unsigned char *buffer)
  218. {
  219. unsigned i,j;
  220. char c;
  221. for(i=0;i<3;i++) 
  222.   if(!biosdisk(3,drive,head,track,sector,nsects,buffer)) return;
  223. printf("\nWrite error: drive %02x, head %u, track %u\n",
  224.        drive,head,track);
  225. printf("Writing one sector at a time.\n");
  226. for(j=0;j<nsects;j++) {
  227.   for(i=0;i<3;i++) 
  228.     if(!biosdisk(3,drive,head,track,sector+j,1,buffer)) goto goodsec;
  229.   printf("Bad sector: drive %02x, head %u, track %u, sector %u\n",
  230.      drive,head,track,sector+j);
  231.   goodsec:
  232.   buffer+=secsize;
  233.   }    
  234. }
  235.